package com.cencat.driver.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * Swagger配置类
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Configuration
public class SwaggerConfig {

    @Value("${server.port:8082}")
    private String serverPort;

    @Value("${server.servlet.context-path:/driver}")
    private String contextPath;

    /**
     * OpenAPI配置
     */
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(apiInfo())
                .servers(List.of(
                        new Server().url("http://localhost:" + serverPort + contextPath).description("本地开发环境"),
                        new Server().url("https://api.cencat.com" + contextPath).description("生产环境")
                ))
                .components(new Components()
                        .addSecuritySchemes("Bearer", securityScheme())
                )
                .addSecurityItem(new SecurityRequirement().addList("Bearer"));
    }

    /**
     * API信息
     */
    private Info apiInfo() {
        return new Info()
                .title("司机端系统API文档")
                .description("CenCat SaaS平台 - 司机端系统接口文档")
                .version("1.0.0")
                .contact(new Contact()
                        .name("cencat")
                        .email("cencat@example.com")
                        .url("https://www.cencat.com")
                )
                .license(new License()
                        .name("Apache 2.0")
                        .url("https://www.apache.org/licenses/LICENSE-2.0")
                );
    }

    /**
     * 安全方案
     */
    private SecurityScheme securityScheme() {
        return new SecurityScheme()
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("JWT")
                .description("请输入JWT Token，格式：Bearer {token}");
    }
}